﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2010-11-20 13:29:36Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Windows;

using Outcoder.UI.Xaml.Controls.Design;

using Microsoft.Phone.Shell;

namespace Outcoder.UI.Xaml.Controls
{
    public abstract class AppBarIconButtonBase : FrameworkElement, IAppBarItem,
        IApplicationBarIconButton, IApplicationBarIconButtonProvider
    {
        protected AppBarIconButtonBase()
        {
			Button = new ApplicationBarIconButton { 
				Text = "Text", 
				/* IconUri must be set. */
				IconUri = new Uri("/Images/Unknown.png", UriKind.Relative) };

			Loaded += HandleLoaded;
			Unloaded += HandleUnloaded;
        }

		void HandleLoaded(object sender, RoutedEventArgs e)
		{
			Button.Click -= ApplicationBarIconButtonClick;
			Button.Click += ApplicationBarIconButtonClick;
		}

		void HandleUnloaded(object sender, RoutedEventArgs e)
		{
			Button.Click -= ApplicationBarIconButtonClick;
		}

        #region IsEnabled

        public static readonly DependencyProperty IsEnabledProperty
            = DependencyProperty.RegisterAttached(
                "IsEnabled",
                typeof(bool),
                typeof(AppBarIconButtonBase),
                new PropertyMetadata(true, HandleEnabledChanged));

        static void HandleEnabledChanged(
            DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AppBarIconButtonBase iconButton;
            if (e.NewValue != e.OldValue
                && (iconButton = d as AppBarIconButtonBase) != null
                && iconButton.Button != null)
            {
                iconButton.Button.IsEnabled = (bool)e.NewValue;
            }
        }

        public bool IsEnabled
        {
            get
            {
                return (bool)GetValue(IsEnabledProperty);
            }
            set
            {
                SetValue(IsEnabledProperty, value);
            }
        }

        #endregion

        #region Text

        public static readonly DependencyProperty TextProperty
            = DependencyProperty.RegisterAttached(
                "Text",
                typeof(string),
                typeof(AppBarIconButtonBase),
                new PropertyMetadata(HandleTextChanged));

        static void HandleTextChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            AppBarIconButtonBase iconButton;
            if (e.NewValue != e.OldValue
                && (iconButton = d as AppBarIconButtonBase) != null
                && iconButton.Button != null)
            {
                iconButton.Button.Text = e.NewValue.ToString();
            }
        }

        void IAppBarItem.PerformClick()
        {
            if (!IsEnabled)
            {
                throw new InvalidOperationException("Element not enabled.");
            }
            ApplicationBarIconButtonClick(this, EventArgs.Empty);
        }

        public string Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }
            set
            {
                SetValue(TextProperty, value);
            }
        }

        #endregion

        #region IconUri

        public static DependencyProperty IconUriProperty
            = DependencyProperty.Register(
                "IconUri",
                typeof(Uri),
                typeof(AppBarIconButtonBase),
                new PropertyMetadata(HandleIconUriChanged));

        static void HandleIconUriChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AppBarIconButtonBase iconButton;
            if (e.NewValue != e.OldValue
                && (iconButton = d as AppBarIconButtonBase) != null
                && iconButton.Button != null)
            {
                try
                {
                    iconButton.Button.IconUri = (Uri)e.NewValue;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }

        public Uri IconUri
        {
            get
            {
                return (Uri)GetValue(IconUriProperty);
            }
            set
            {
                SetValue(IconUriProperty, value);
            }
        }

        #endregion

        #region Click Event

        public event EventHandler Click;

        void ApplicationBarIconButtonClick(object sender, EventArgs e)
        {
            OnClick(e);
        }

        protected virtual void OnClick(EventArgs e)
        {
            var tempEvent = Click;
            if (tempEvent != null)
            {
                tempEvent(this, e);
            }
        }

        public ApplicationBarIconButton Button { get; set; }

        #endregion
    }
}